<?php
	require_once ( 'AbstractDAO.php' );
	
	/**
	 * Description of FriendDAO
	 *
	 * @author Andrew
	 */
	class FriendDAO extends AbstractDAO 
	{
		function SearchFriend($sid, $tid)
		{
			$sql = "select friends from user_friend where user_id=$sid";
			$res = $this->db->GetValue($sql);
			
			if( preg_match( "/$tid:.+? /", $res ) == '1' )
				return true;
			
			return false;
		}
		
		function AddFriend($sid, $tid)
		{
			if($this->SearchFriend($sid, $tid)==true)
				return false;
			else
			{
				$sql = "select friends from user_friend where user_id=$sid";
				$res = $this->db->GetValue($sql);
				$userDAO = GetUserDAO();
				$nick = $userDAO->GetUserNameByID( $tid );
				$res.="$tid:$nick ";					
				$sql="update user_friend set friends = '$res' where user_id = $sid ";
				$this->db->Update($sql);
				return true;
			}
		}
		
		function DelFriend($sid, $tid)
		{
			$sql = "select friends from user_friend where user_id=$sid";
			$res = $this->db->GetValue($sql);
			if($res===false)
				return false;
			$res = preg_replace( "/$tid:.+? /", "", $res );	
			$sql="update user_friend set friends = '$res' where user_id = $sid ";
			$this->db->Update($sql);
			return true;
		}
		
		function GetFriendCountByUid($uid)
		{
			$sql = "select friends from user_friend where user_id=$uid";
			$res = $this->db->GetValue($sql);
			if($res==="")
				return '0';
			else
			{	
				$res = split(" ", trim($res) );
				return count($res);
			}
		}
		
		private function GetFriendsIdList( $uid )
		{
			$sql="select friends from user_friend where user_id=$uid";
			$res=$this->db->GetValue($sql);	
			$id_list = "";
			if( trim($res) == "")
				return $id_list;

			$res = split(" ", trim($res));
			foreach( $res as $key => $value )
			{
				$f=split(":", $value);
				$id_list.=$f['0'].",";
			}			
			$id_list = rtrim( $id_list, ',' );
			return $id_list;
		}
		
		function GetFriendsNicks($uid)
		{
			$id_list = $this->GetFriendsIdList($uid);
			
			if( $id_list==="")
				return false;
			
			$sql = " select id, nick
					 from user_basic_info
					 where id in( $id_list )";
			
			return $this->db->GetPlan($sql);
		}
		
		function GetFriendsByUid($uid, $sort='nick', $order='asc', $cpage='1',$perpage='12')
		{
			
			$id_list = $this->GetFriendsIdList($uid);
			
			if($id_list ==="")
				return false;
			else
			{
				//$id_str=implode(',',$id_list);	
				
				$sql="select id, nick, logo_path, location, 
				(select contribution from user_statistic where user_id= user_basic_info.id) as contribution 
				from user_basic_info where id in ($id_list)";		
				$sql=$sql." order by $sort";				

				return $this->getRows($sql, $perpage, $cpage);		
			}
		}
		
		function GetFriendEventByUid($uid, $count='20')
		{
			$id_list = $this->GetFriendsIdList($uid);	
			if($id_list == "")
				return false;
			else
			{
				$sql="select ue.user_id, ubi.nick, ue.action, ue.parameters, TIMESTAMPDIFF( MINUTE, ue.date, NOW()) 
				as timediff from user_event as ue, user_basic_info as ubi where ubi.id=ue.user_id and ue.user_id  in (".$id_list.") order by ue.date desc limit 0, $count";
				$result=$this->db->GetPlan($sql);
				if(!$result)
					return false;
				foreach($result as $key=>$event)
				{
					if($event['timediff']>=0&&$event['timediff']<60)
						$event['timediff']=$event['timediff'].'分钟前';
					else
					{
						if($event['timediff']>=60&&$event['timediff']<1440)
							$event['timediff']=floor($event['timediff']/60).'小时前';
						else
						{
							if($event['timediff']>=1440&&$event['timediff']<43200)
								$event['timediff']=floor($event['timediff']/1440).'天前';
							else
								$event['timediff']=floor($event['timediff']/43200).'月前';
						}
					}
					$eventList[]=$event;
				}
				return $eventList;
			}
		}
		
	}
?>